package com.shine.app.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shine.app.R;
import com.shine.app.commons.SpecialCalendar;

public class CalendarAdapter extends BaseAdapter {
	private boolean isLeapyear = false;
	private int daysOfMonth = 0;
	private int dayOfWeek = 0;
	private int lastDaysOfMonth = 0;
	private String[] dayNumber = new String[42];
	private SpecialCalendar specialCalendar = null;
	private LayoutInflater inflater;

	private int currentYear;
	private int currentMonth;
	private int currentDay;
	private int selectYear;
	private int selectMonth;
	private int selectDay;

	private String showYear = "";
	private String showMonth = "";

	static class ViewHolder {
		private LinearLayout itembg;
		private TextView dayText;
	}

	public CalendarAdapter() {
	}

	public CalendarAdapter(Context context, int jumpMonth, int jumpYear,
			int year, int month, int day, int selectYear, int selectMonth,
			int selectDay) {
		this();
		inflater = LayoutInflater.from(context);
		specialCalendar = new SpecialCalendar();
		int stepYear = year + jumpYear;
		int stepMonth = month + jumpMonth;
		if (stepMonth > 0) {
			if (stepMonth % 12 == 0) {
				stepYear = year + stepMonth / 12 - 1;
				stepMonth = 12;
			} else {
				stepYear = year + stepMonth / 12;
				stepMonth = stepMonth % 12;
			}
		} else {
			stepYear = year - 1 + stepMonth / 12;
			stepMonth = stepMonth % 12 + 12;
			if (stepMonth % 12 == 0) {
			}
		}
		this.currentYear = year;
		this.currentMonth = month;
		this.currentDay = day;
		this.selectYear = selectYear;
		this.selectMonth = selectMonth;
		this.selectDay = selectDay;
		getCalendar(stepYear, stepMonth);
	}

	public CalendarAdapter(Context context, int year, int month, int day,
			int selectYear, int selectMonth, int selectDay) {
		this();
		inflater = LayoutInflater.from(context);
		specialCalendar = new SpecialCalendar();
		this.currentYear = year;
		this.currentMonth = month;
		this.currentDay = day;
		this.selectYear = selectYear;
		this.selectMonth = selectMonth;
		this.selectDay = selectDay;
		getCalendar(currentYear, currentMonth);
	}

	@Override
	public int getCount() {
		return dayNumber.length;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.calendar_item, null);
			holder.dayText = (TextView) convertView.findViewById(R.id.tv_day);
			holder.itembg = (LinearLayout) convertView
					.findViewById(R.id.item_bg);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		String day = dayNumber[position];
		holder.dayText.setText(day);
		holder.dayText.setTextColor(Color.TRANSPARENT);
		holder.dayText.setTag(false);

		int item_year = Integer.parseInt(getShowYear());
		int item_month = Integer.parseInt(getShowMonth());
		int item_day = Integer.parseInt(day);

		if (position < daysOfMonth + dayOfWeek && position >= dayOfWeek) {
			holder.dayText.setTextColor(Color.WHITE);
			holder.dayText.setTag(true);
			if (item_year < currentYear
					|| (item_year == currentYear && item_month < currentMonth)
					|| (item_year == currentYear && item_month == currentMonth && item_day < currentDay)) {
				holder.dayText.setTag(false);
				holder.dayText.setTextColor(Color.LTGRAY);
			} else if (currentMonth != 12) {
				if ((item_year > currentYear)
						|| (item_year == currentYear && (item_month > currentMonth + 1))
						|| (item_year == currentYear
								&& (item_month == currentMonth + 1) && (item_day > currentDay))) {
					holder.dayText.setTag(false);
					holder.dayText.setTextColor(Color.LTGRAY);
				}
			} else if (currentMonth == 12) {
				if ((item_year > (currentYear + 1))
						|| (item_year == (currentYear + 1) && (item_month > 1))
						|| (item_year == (currentYear + 1) && (item_month == 1) && (item_day > currentDay))) {
					holder.dayText.setTag(false);
					holder.dayText.setTextColor(Color.LTGRAY);
				}
			}
		}

		if (selectYear == Integer.parseInt(getShowYear())
				&& selectMonth == Integer.parseInt(getShowMonth())
				&& selectDay == Integer.parseInt(day)) {
			if ((Boolean) holder.dayText.getTag()) {
				holder.itembg
						.setBackgroundResource(R.drawable.searchlocation_player_select);
			}
		} else {
			holder.itembg
					.setBackgroundResource(R.drawable.searchlocation_player_noselect);
		}

		return convertView;
	}

	public void getCalendar(int year, int month) {
		isLeapyear = specialCalendar.isLeapYear(year);
		daysOfMonth = specialCalendar.getDaysOfMonth(isLeapyear, month);
		dayOfWeek = specialCalendar.getWeekdayOfMonth(year, month) - 1;
		if (dayOfWeek == -1) {
			dayOfWeek = 6;
		}
		lastDaysOfMonth = specialCalendar.getDaysOfMonth(isLeapyear, month - 1);
		getweek(year, month);
	}

	private void getweek(int year, int month) {
		int j = 1;
		for (int i = 0; i < dayNumber.length; i++) {
			if (i < dayOfWeek) {
				int temp = lastDaysOfMonth - dayOfWeek + 1;
				dayNumber[i] = (temp + i) + "";
			} else if (i < daysOfMonth + dayOfWeek) {
				String day = String.valueOf(i - dayOfWeek + 1);
				dayNumber[i] = i - dayOfWeek + 1 + "";
				setShowYear(String.valueOf(year));
				setShowMonth(String.valueOf(month));
			} else {
				dayNumber[i] = j + "";
				j++;
			}
		}
		String abc = "";
		for (int i = 0; i < dayNumber.length; i++) {
			abc = abc + dayNumber[i] + ":";
		}
	}

	public String getDateByClickItem(int position) {
		return dayNumber[position];
	}

	public int getStartPositon() {
		return dayOfWeek + 7;
	}

	public int getEndPosition() {
		return (dayOfWeek + daysOfMonth + 7) - 1;
	}

	public String getShowYear() {
		return showYear;
	}

	public void setShowYear(String showYear) {
		this.showYear = showYear;
	}

	public String getShowMonth() {
		return showMonth;
	}

	public void setShowMonth(String showMonth) {
		this.showMonth = showMonth;
	}

	public int getSelectDay() {
		return selectDay;
	}

	public void setSelectDay(int selectDay) {
		this.selectDay = selectDay;
	}

	public int getSelectYear() {
		return selectYear;
	}

	public void setSelectYear(int selectYear) {
		this.selectYear = selectYear;
	}

	public int getSelectMonth() {
		return selectMonth;
	}

	public void setSelectMonth(int selectMonth) {
		this.selectMonth = selectMonth;
	}

}
